#include <stdio.h>
#include <stdbool.h>
#include <fcntl.h>
#include "clang-c/Index.h"
#include <stdbool.h>
#include <fcntl.h>


char* findingString = "Init";
int numOfArgc = 0;

bool printKindSpelling(CXCursor cursor) {
    enum CXCursorKind curKind = clang_getCursorKind(cursor);
    const char *curkindSpelling = clang_getCString(
                                  clang_getCursorKindSpelling(curKind));
    if(curKind==4)
    printf("**************");
    if(curKind==44)
    printf("44444444444");
    printf("kind: %s\n", curkindSpelling);
    return true;
}

bool printSpelling(CXCursor cursor) {
    const char *astSpelling = clang_getCString(clang_getCursorSpelling(cursor));
    printf("name: %s  ", astSpelling);
    return true;
}


enum CXChildVisitResult printVisitor(CXCursor cursor, CXCursor parent,
                                     CXClientData client_data) {
    char *astSpelling = clang_getCString(clang_getCursorSpelling(cursor));
     
   enum CXCursorKind curKind = clang_getCursorKind(cursor);
      
       if(curKind==4)
     {
        printSpelling(cursor);
        printKindSpelling(cursor);
        clang_visitChildren(cursor, printVisitor, NULL);
 
 
        return CXChildVisit_Continue;
     }
    else if(curKind==44)
    {
        printSpelling(cursor);
        printKindSpelling(cursor);
     
    	return CXChildVisit_Continue;
     }
    // return CXChildVisit_Recurse;
}

int main(int argc, char *argv[]) {

        int result = open(argv[1], O_RDONLY);  //异常处理：打不开要分析的文件的情况
        if (result == -1) {
            printf("Can't open the file: %s.\n", argv[1]);
            return -1;
        }

    numOfArgc = argc;

    CXIndex Index = clang_createIndex(0,0);
    CXTranslationUnit TU = clang_parseTranslationUnit(Index, 0, argv, argc,
                                                      0, 0,
                                                      CXTranslationUnit_None);
    CXCursor C = clang_getTranslationUnitCursor(TU);

    printSpelling(C);
    printKindSpelling(C);

    clang_visitChildren(C, printVisitor, NULL);

    clang_disposeTranslationUnit(TU);
    clang_disposeIndex(Index);
    return 0;
}
